package com.db.bankapp.domain;

import com.db.bankapp.exception.NotEnoughFundsException;
import com.db.bankapp.exception.OverDraftLimitExceedException;

/**
 * Created by Student on 18.08.2014.
 */
class CheckingAccount extends AccountBase {
    private int overdraft;

    public int getOverdraft() {
        return overdraft;
    }

    public CheckingAccount(int balance, int overdraft) {
        this.balance = balance;
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(int amount) throws OverDraftLimitExceedException {
        try {
            super.withdraw(amount);
        } catch (NotEnoughFundsException e) {
            throw new OverDraftLimitExceedException(e, amount, balance, overdraft);
        }
        if (balance <0) {
            overdraft = overdraft + balance;
            balance = 0;
            System.out.println("Changing credit limit to " + overdraft);
        }
    }

    @Override
    public int maximumAmountToWithdraw() {
        return balance + overdraft;
    }

    @Override
    public boolean equals(Object o) {
        boolean bool = super.equals(o);
        if (o instanceof CheckingAccount) return bool && (this.overdraft==((CheckingAccount)o).overdraft);
        return false;
    }
    @Override
    public int hashCode() {
        int result=super.hashCode();
        return result*31+this.overdraft;
    }
    @Override
    public String toString() {
        StringBuffer string = new StringBuffer(super.toString()).append(" and overdraft " + overdraft +"$");
        return string.toString();
    }
}
